﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindowsFormsApplication1.API;
using Millionaire;
namespace WindowsFormsApplication1.BLL
{
    public class ChiTietLuotChoiBLL : BaseBLL, IChiTietLuotChoiBLL
    {
        #region IChiTietLuotChoiBLL Members

        public bool AddNew(ChiTietLuotChoi chiTiet)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                try
                {
                    ctxt.ChiTietLuotChois.InsertOnSubmit(chiTiet);
                    ctxt.SubmitChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

       

        public bool Delete(ChiTietLuotChoi chiTiet)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                try
                {
                    ChiTietLuotChoi delObj = ctxt.ChiTietLuotChois.SingleOrDefault(x => x.MaChiTiet == chiTiet.MaChiTiet);
                    if (delObj != null)
                    {
                        ctxt.ChiTietLuotChois.DeleteOnSubmit(delObj);
                        ctxt.SubmitChanges();
                        return true;
                    }
                    else
                        return false;

                }
                catch (Exception)
                {
                    return false;
                }

            }
        }

        public ChiTietLuotChoi GetByID(int idChiTiet)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.ChiTietLuotChois.SingleOrDefault(x => x.MaLuotChoi == idChiTiet);
            }
        }

        public List<ChiTietLuotChoi> GetAll()
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.ChiTietLuotChois.ToList();
            }
        }

        public List<ChiTietLuotChoi> GetByLuotChoi(int idLuotChoi)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.ChiTietLuotChois.Where(x => x.MaLuotChoi == idLuotChoi).ToList();
            }
        }


        public List<ChiTietLuotChoi> GetByCauHoi(int idCauHoi)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.ChiTietLuotChois.Where(x => x.MaCauHoi == idCauHoi).ToList();
            }
        }

        public ChiTietLuotChoi GetByCauHoiLuotChoi(int idCauHoi, int idLuotChoi)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.ChiTietLuotChois.SingleOrDefault(x => x.MaCauHoi == idCauHoi && x.MaLuotChoi == idLuotChoi);
            }
        }

        #endregion



        public bool Update(ChiTietLuotChoi chitiet)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                try
                {
                    ChiTietLuotChoi edit = ctxt.ChiTietLuotChois.SingleOrDefault(x => x.MaChiTiet == chitiet.MaChiTiet);
                    if (edit != null)
                    {
                        edit.MaCauHoi = chitiet.MaCauHoi;
                        ctxt.SubmitChanges();
                        return true;
                    }
                    else
                        return false;

                }
                catch (Exception)
                {
                    return false;
                }

            }
        }
    }
}
